<?php
if (!defined('DC_RC_PATH')) { return;}

$customer 	= new Customer($core->blog);
if($_GET['logout']==1){
	$customer->logout();
	header('Location:'.$core->blog->url);
}elseif (!empty($_POST['customerAuthRegister']) && !empty($_POST['customersAuthLogin']) 
		&& !empty($_POST['customersAuthMail'])&&!empty($_POST['customersAuthPassword'])){
	$customer->create($_POST['customersAuthLogin'],$_POST['customersAuthMail'],$_POST['customersAuthPassword']);
}elseif ( !empty($_POST['customersAuthLogin']) && !empty($_POST['customersAuthPassword']) && empty($_POST['customerAuthRegister'])){
	$customer->log_in($_POST['customersAuthLogin'],$_POST['customersAuthPassword'],$_POST['customersAuthRemember']);
}else{
	$customer->is_Loged();
}

$core->url->register('customersAuth-icons','customersAuth/icons','^customersAuth/icons/(.*)$',array('publicCustomersAuth','iconURL'));
$core->addBehavior('publicHeadContent',array('publicCustomersAuth','publicHeadContent'));
$core->addBehavior('coreBlogGetPosts',array('rsExtPostCustomerAuth','coreBlogGetPosts'));
//$core->addBehavior('corePostSearch',array('Customer','PostSearch'));

$core->tpl->addValue('customersAuth',array('publicCustomersAuth','customersAuthWidget'));
$core->tpl->addBlock('Entries',array('tplDcEntries','Entries'));

class tplDcEntries extends dcTemplate{
	public function Entries($attr,$content){
		global $core;
		
		$categPermission = false;
		if($_SESSION['uid']){
			$customer = New Customer($core);
			$customer->init($_SESSION['uid']);
			$categPermission =$customer->get_permissions($rs->cat_id);
		}else{
			$permission = new Permission($core);
			$categPermission = $permission->get_permission(0,$rs->cat_id);
		}
			
		if($categPermission){
			return $content;
		}else{
			return '';
		}
	}
}


// GESTION DE L'AFFICHAGE DU WIDGET
class publicCustomersAuth
{
	public static function publicHeadContent(&$core) {
		echo '<style type="text/css">'."\n".self::customersAuthStyle()."\n</style>\n";
		echo '<script type="text/javascript">'.
			 '	function openPopUp(){
			 		pp = document.getElementById("popup");
		 			pp.style.visibility="visible";
		 			pp.style.display="block";
			 	}
			 	function closePopUp(){
			 		pp = document.getElementById("popup");
		 			pp.style.visibility="hidden";
		 			pp.style.display="none";
			 	}
			 '.
			 '</script>';
	}
	
	public static function customersAuthStyle()	{
		global $core;
		
		$css = array();
		

		/* Properties
		---------------------------------------------- */
		self::prop($css,'div#customersAuth label','width','80px');
		self::prop($css,'div#customersAuth label','float','left');
		self::prop($css,'div#customersAuth input','width','110px');
		self::prop($css,'div#customersAuth input','margin-top','1px');
		self::prop($css,'div#customersAuth input','border','1px solid grey');
		self::prop($css,'div#customersAuth .c_remember','border','0 none');
		self::prop($css,'div#customersAuth .c_remember','vertical-align','text-bottom');
		self::prop($css,'div#customersAuth .c_remember','margin-left','0px');
		self::prop($css,'div#customersAuth .c_remember','margin-right','5px');
		self::prop($css,'div#customersAuth .c_remember','width','auto');
		self::prop($css,'div#customersAuth .submit','font-size','1em');
		self::prop($css,'div#customersAuth .submit','font-weight','bold');
		self::prop($css,'div#customersAuth .submit','text-transform','uppercase');
		self::prop($css,'div#customersAuth .submit','border','0 none');
		self::prop($css,'div#customersAuth .submit','width','auto');
		self::prop($css,'div#customersAuth .submit','float','right');
		self::prop($css,'div#customersAuth .submit','background','transparent');
		
		self::prop($css,'div#popup','position','fixed');
		self::prop($css,'div#popup','margin','0');
		self::prop($css,'div#popup','padding','0');
		self::prop($css,'div#popup','width','100%');
		self::prop($css,'div#popup','top','0px');
		self::prop($css,'div#popup','height','100%');
		self::prop($css,'div#popup','left','0px');
		self::prop($css,'div#popup','display','none');
		self::prop($css,'div#popup','visibility','hidden');
		self::prop($css,'div#popup','z-index','100');
		self::prop($css,'div#popup','background','url('.$core->blog->url.'customersAuth/icons/fb.png) repeat');
		
		self::prop($css,'div#ppcontent','position','absolute');
		self::prop($css,'div#ppcontent','margin','0');
		self::prop($css,'div#ppcontent','padding','0');
		self::prop($css,'div#ppcontent','width','202px');
		self::prop($css,'div#ppcontent','height','150px');
		self::prop($css,'div#ppcontent','padding','10px');
		self::prop($css,'div#ppcontent','top','50%');
		self::prop($css,'div#ppcontent','margin-top','-75px');
		self::prop($css,'div#ppcontent','left','50%');
		self::prop($css,'div#ppcontent','margin-left','-101px');
		self::prop($css,'div#ppcontent','border','1px solid grey');
		self::prop($css,'div#ppcontent','background-color','white');
		self::prop($css,'div#ppcontent','z-index','101');

		self::prop($css,'div#ppcontent img','float','right');
		self::prop($css,'div#ppcontent a','clear','both');
		self::prop($css,'div#ppcontent a','width','100%');
		self::prop($css,'div#ppcontent a','float','left');
		self::prop($css,'div#ppcontent p','margin-top','35px');
		
		self::prop($css,'div#ppcontent label','width','80px');
		self::prop($css,'div#ppcontent label','float','left');
		self::prop($css,'div#ppcontent input','width','110px');
		self::prop($css,'div#ppcontent input','margin-top','1px');
		self::prop($css,'div#ppcontent input','border','1px solid grey');
		self::prop($css,'div#ppcontent input.submit','float','right');
		
		self::prop($css,'div#error','position','absolute');
		self::prop($css,'div#error','background-color','red');
		self::prop($css,'div#error','color','black');
		self::prop($css,'div#error','top','0px');
		self::prop($css,'div#error','left','0px');
		self::prop($css,'div#error','width','100%');
		
		$res = '';
		foreach ($css as $selector => $values) {
			$res .= $selector." {\n";
			foreach ($values as $k => $v) {
				$res .= $k.':'.$v.";\n";
			}
			$res .= "}\n";
		}
		return $res;
	}
	
	protected static function prop(&$css,$selector,$prop,$value)
	{
		if ($value) {
			$css[$selector][$prop] = $value;
		}
	}
	
	public static function customersAuthWidget(&$w){
		global $core,$crypt;
		
		$customer 	= new Customer($core->blog);
		$group		= new Group($core->blog);
		
		// Si nous sommes pas en page accueil et que c'est coché page accueil uniquement on fait rien
		if ($w->homeonly && $core->url->type != 'default') {
			return;
		}
		
		$title = $w->title ? html::escapeHTML($w->title) : __('CustomersAuth');
		
		$output='';
		// on verifie si la personne est déjà loggé	

		if($customer->is_Loged()){
			$output .= '<div id="customersAuth">'.
						'<h2>'.$title.'</h2>'.
						'Bienvenue '.$customer->login;
			if($customer->group_id == 0){
				$output.= '<br/>Vous êtes en attente d\'autorisation par l\'administrateur';
			}else{
				$output.= '<br/>Vous avez accès a l\'espace supplémentaire "'.$group->get_groupName($customer->group_id).'" ';
			}
			$output.= '<br/>pour changer votre mot de passe <a href="">cliquez ici</a>'.
						'<br/><a href="?logout=1" >Déconnexion</a>'.
						'</div>';
		}else{
			$output .= 
				'<div id="customersAuth">'.
					'<h2>'.$title.'</h2>'.
					'<form method="post" action="'.$core->blog->url.'">'.
					'<p><label>'.__('Pseudo:').'</label><input type="text" name="customersAuthLogin" /><br />'.
					'<label>'.__('Password:').'</label><input type="password" name="customersAuthPassword" /><br />'.
					'<label>&nbsp;</label><input class="c_remember" type="checkbox" name="customersAuthRemember" />'.__('Remember').
					'<input class="submit" type="submit" value="ok" /></p>'.
					'<p><a href="#" onClick="openPopUp();return false;">'.__('Subscribe').'</a></p>'.
					'</form>'.
				'</div>';
		}
		$output .= '<div id="error">'.$core->error->toHTML().'</div>'.
					'<div id="popup">'.
					'<div id="ppcontent">'.
					'<a href="#" onClick="closePopUp();return false;"><img src="'.$core->blog->url.'customersAuth/icons/x.gif" alt="fermer"/></a>'.
					'<form method="post" action="'.$core->blog->url.'" name="register">'.
					'<input type="hidden" name="customerAuthRegister" value="1"/>'.
					'<p><label>'.__('Pseudo:').'</label><input type="text" name="customersAuthLogin" /><br />'.
					'<label>'.__('Password:').'</label><input type="password" name="customersAuthPassword" /><br />'.
					'<label>'.__('Mail adress:').'</label><input type="text" name="customersAuthMail" /><br />'.
					'<input class="submit" type="submit" value="ok" /></p>'.
					'</form></div></div>';
		return $output;

	}
	
	public static function iconURL($arg){
		$file = dirname(__FILE__).'/icons/'.$arg;
		if (!file_exists($file)) {
			http::head(404,'Not Found');
			exit;
		}
		
		http::cache(array_merge(array($file),get_included_files()));
		
		header('Content-Type: image/png');
		readfile($file);
		exit;
	}
}
?>